<?php
/**
 * --------------------------------------------------------------------
 * MCore框架
 * 作者：阿枫，QQ：52037872，Email：nczy520@gmail.com
 * --------------------------------------------------------------------
 *
 * SVN revision information:
 * @version $Revision: 50 $:
 * @author  $Author: zy $:
 * @date      $Date: 2013-11-09 19:34:15 +0800 (星期六, 09 十一月 2013) $:
 * @url         $HeadURL: svn://vps.mdeve.com/web_mcore/manage/function/global.func.php $:
 */

class _IndexClass extends __basic_def {

    function _main() {
        global $_M, $row, $template;

        $_M->template();

        // 读取系统菜单
        $menu = "";
        $row = $_M->db->get_rows(array("table"=>"nav", "where"=>"hidden=0", "order"=>"sort"));
        // 如果插件允许，则在此添加插件菜单内容
        if($_M->module_enable && is_array($_M->module_installed)) {
            foreach($_M->module_installed as $module) {
                $module_file = MODULE_PATH. DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR . "module.php";
                if(file_exists($module_file)) {
                    $module_class_name = "_" . ucfirst($module) . "ModuleClass";
                    $interface = new ReflectionClass("_module_interface");
                    if (class_exists($module_class_name)) {
                        $module_class = new ReflectionClass($module_class_name);
                        if ($module_class->isSubclassOf($interface)) {
                            $module = $module_class->newInstance();
                            $method = $module_class->getMethod("manage_menus");
                            $module_menus = $method->invoke($module);
                            if (is_array($module_menus) && sizeof($module_menus)) {
                                $row = array_merge($row, $module_menus);
                            }
                        }
                    }
                }
            }
        }

        // 菜单排序
        $key_arrays = array();
        foreach($row as $r) {
            $key_arrays[] = $r["sort"];
        }
        array_multisort($key_arrays, SORT_ASC, SORT_NUMERIC, $row);

        if($_M->cfg["module_enable"]) module_insert_code('manage_index_menu_create');

        foreach($row as $r1) {
            if(!$r1["pid"]) {
                // 一级菜单
                if($this->has_child_node($row, $r1["id"])) {
                    // 如果有二级菜单
                    $main_menu2 = $this->get_content_from_template("menu_1_has_child", $r1);
                    $main_menu2_list = "";
                    foreach($row as $r2) {
                        if($r2["pid"] == $r1["id"]) {
                            if($this->has_child_node($row, $r2["id"])) {
                                // 如果有三级菜单
                                $main_menu3 = $this->get_content_from_template("menu_2_has_child", $r2);
                                $main_menu3_list = "";
                                foreach ($row as $r3) {
                                    if($r3["pid"] == $r2["id"]) {
                                        $main_menu3_list .= $this->get_content_from_template("menu_list", $r3);
                                    }
                                }
                                $main_menu2_list .= str_replace("{main_menu3_define}", $main_menu3_list, $main_menu3);
                            } else {
                                $main_menu2_list .= $this->get_content_from_template("menu_list", $r2);
                            }
                        }
                    }
                    $menu .= str_replace("{main_menu2_define}", $main_menu2_list, $main_menu2);

                } else {
                    $menu .= $this->get_content_from_template("menu_1_no_child", $r1);
                }
            }

        }

        $template = MANAGE_PATH."/templates/main/index";
        if($_M->cfg["module_enable"]) module_insert_code('manage_index_load_template');

        $_M->template->load($template);
        $_M->template->set_var("{main_menu_define}", $menu);
        if($_M->cfg["module_enable"]) module_insert_code('manage_index_success');
        $_M->template->display();
    }


    /**
     * 判断某节点是否还有子节点
     *
     * @param $row
     * @param $id
     * @return bool
     */
    private function has_child_node($row, $id){
        foreach($row as $r) {
            if($r["pid"] == $id) {
                return true;
            }
        }
        return false;
    }

    private function get_content_from_template($template, $row){
        global $_M;

        $_M->template->load(MANAGE_PATH. "/templates/main/". $template);
        $_M->template->set_vars($row);
        return $_M->template->display(true);
    }

}